<?php
/**
 * Copyright (c) 2023-2222   All rights reserved.
 *
 * 创建时间：2023-01-24 13:38
 *
 * 项目：levs  -  $  - ${FILE_NAME}
 *
 * 作者：liwei
 */

//!defined('INLEV') && exit('Access Denied LEV');


namespace modules\levmodules\helpers\generates;

use Lev;
use lev\helpers\ModulesHelper;
use modules\levmodules\helpers\generateControllerHelper;

class BaseCtypes
{

    public static function callCtype($ctype, $iden, $info, array $replaces = [], $forceGen = false)
    {

        $method = 'ctype'.$ctype;
        if (method_exists(__CLASS__, $method)) {
            $ckModule = generateControllerHelper::isExistIden($idenDir = ModulesHelper::getIdenDir($iden));
            if (!$ckModule) {
                generateControllerHelper::$tips .= '请先创建模块 ';
                return Lev::responseMsg(-10111, '请先创建模块');
            }
            $moduleIdenDir = Lev::$aliases['@modules'] . '/' . $idenDir;
            $route = explode("/", $info['link'])[0];
            if (!$route) {
                return Lev::responseMsg(-10223, '路由名称不能为空且不能以"/"开头');
            }
            $className = Lev::ucfirstv($route);
            $ckController = $moduleIdenDir . '/controllers/_gen/Base'.$className.'.php';
            if (!$forceGen && is_file($ckController)) {
                generateControllerHelper::$tips .= ' <tips>控制器已经创建过了！</tips>'.basename($ckController).'<br>';
                return Lev::responseMsg(-10111, '控制器已经创建过了！'.$ckController);
            }
            $responseMsg = static::$method($iden, $moduleIdenDir, $className, $route, $replaces, $info, $forceGen);
        }else {
            $responseMsg = Lev::responseMsg(-4, '类型方法不存：'.$ctype);
        }
        $responseMsg['files'] = generateControllerHelper::$generateFiles;
        return $responseMsg;
    }

    /**
     * 批量注册用户控制器
     */
    public static function ctype2($iden, $moduleIdenDir, $className, $route, $replaces) {
        $generateDir = dirname(__DIR__) . '/../_generates/_controllers/_admin_register_user';
        $replaces = generateControllerHelper::replaceVars($iden, $replaces);
        $replaces['AdminRegcUser']   = $className;
        $replaces['admin-regc-user'] = $route;
        $unFiles[$moduleIdenDir . '/controllers/'.$className.'Controller.php'] = 1;

        generateControllerHelper::doGenerateFiles($generateDir, $moduleIdenDir, array_keys($replaces), array_values($replaces), $unFiles);
        generateControllerHelper::$tips .= ' <span class="color-orange">批量注册控制器文件创建成功：Base'.$className.'</span><br>';
        return Lev::responseMsg(2, '成功');
    }


}